package com.elsa.DataStatistics.statistics;

import com.elsa.DataStatistics.mapper.DayMapper;
import com.elsa.DataStatistics.mapper.MenuMapper;
import com.elsa.DataStatistics.mapper.MenuOrderMapper;
import com.elsa.DataStatistics.mapper.OrderMapper;
import com.elsa.DataStatistics.pojo.Day;
import com.elsa.DataStatistics.pojo.Menu;
import com.elsa.DataStatistics.pojo.MenuOrder;
import com.elsa.DataStatistics.pojo.Order;
import com.elsa.DataStatistics.utils.CalculateUtil;
import com.elsa.DataStatistics.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class DayStatistics {

    @Autowired
    private TimeUtil timeUtil;

    @Autowired
    private DayMapper dayMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private MenuOrderMapper menuOrderMapper;

    @Autowired
    private MenuMapper menuMapper;

    public void dayStatistics(String oId) throws InterruptedException {

        int dId = timeUtil.getDayTime();

        Day day1 = dayMapper.selectById(dId);

        Day day2 = new Day();

        if(day1 == null) {
            Order order = orderMapper.selectById(oId);
            Double TotalIncome = 0.0;
            List<MenuOrder> menuOrderList;
            menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            while(menuOrderList.size() == 0) {
                Thread.sleep(200);
                menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            }
            for(int i = 0; i < menuOrderList.size(); i++) {
                TotalIncome += menuOrderList.get(i).getMoPrice() * menuOrderList.get(i).getMoNum();
            }
            log.info("订单总收入：" + TotalIncome);
            Double TotalExpend = 0.0;
            menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            for(int i = 0; i < menuOrderList.size(); i++) {
                int Mid = menuOrderList.get(i).getMId();
                Menu menu = menuMapper.selectById(Mid);
                TotalExpend += menu.getMOldPrice() * menuOrderList.get(i).getMoNum();
            }
            log.info("订单总支出：" + TotalExpend);

            day2.setDId(dId);
            day2.setDIncome(TotalIncome);
            day2.setDExpend(TotalExpend);
            day2.setDProfit(TotalIncome - TotalExpend);
            try {
                dayMapper.insert(day2);
            } catch (Exception e) {
                log.info("插入失败" + e.getMessage());
            }
        } else {
            Order order = orderMapper.selectById(oId);
            Double TotalIncome = 0.0;
            List<MenuOrder> menuOrderList;
            menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            while(menuOrderList.size() == 0) {
                log.info("订单表还没数据，先睡眠200毫秒");
                Thread.sleep(200);
                menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            }
            for(int i = 0; i < menuOrderList.size(); i++) {
                TotalIncome += menuOrderList.get(i).getMoPrice() * menuOrderList.get(i).getMoNum();
            }
            log.info("订单总收入：" + TotalIncome);
            Double TotalExpend = 0.0;
            menuOrderList = menuOrderMapper.selectMenuOrderMapperFromOrderDataStatistics(oId);
            for(int i = 0; i < menuOrderList.size(); i++) {
                int Mid = menuOrderList.get(i).getMId();
                Menu menu = menuMapper.selectById(Mid);
                TotalExpend += menu.getMOldPrice() * menuOrderList.get(i).getMoNum();
            }
            log.info("订单总支出：" + TotalExpend);

            day2.setDId(dId);
            day2.setDIncome(day1.getDIncome() + TotalIncome);
            day2.setDExpend(day1.getDExpend() + TotalExpend);
            day2.setDProfit(day1.getDProfit() + (TotalIncome - TotalExpend));
            try {
                dayMapper.updateById(day2);
                log.info("更新成功");
            } catch (Exception e) {
                log.info("更新失败" + e.getMessage());
            }
        }

    }

}
